<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<script type="text/javascript">
	$(function(){
		$("#resGrid").datagrid({
			url:'${ctx}/res/queryResList.usr?cityId=${cityId}&resType=${resType}',
			toolbar:[{
				text:'取消选择',
				iconCls:'icon-remove',
				handler:function(){
					$('#resGrid').datagrid('clearSelections');					
				}
			},'-'],
			onLoadSuccess:function(data){//加载成功，选中资源
				<c:forEach items="${resIdList}" var="resId">
				$('#resGrid').datagrid('selectRecord','${resId}');
				</c:forEach>	
			},
			onLoadError:function(){
				$.messager.alert('提示','操作超时，请重新选择线路','warning');	
			}
		});	
	});
	
	/**
	 * 如果有选择，则在后台执行新增或者是更新操作;
	 * 如果没有选择，则在后台要先判断是否允许删除，对于标配资源是不允许删除的，
	 * 对于非标配资源则执行删除操作。
	 * 
	 */
	function addOrUpdate(){
		var rows = $('#resGrid').datagrid('getSelections');//json：[{},{}]
		var ps = "";
	    ps += "?routeDetailId=${routeDetailId}&resType=${resType}";
	    $.each(rows,function(i,n){
	    	ps += "&resIds="+n.ID;
	    });
	    $.post('${ctx}/res/updateRes.usr'+ps,function(data){
	        if(data.success){//操作成功
				$('#settingWin').window('close');
	        	var content='';
	        	for(var i=0;i<rows.length;i++){
	        		var j=1;
	        		for(var key in rows[i]){
	        			if(j==2){
	        				content +=rows[i][key]+"&nbsp;";
	        				break;
	        			}
	        			j++;
	        		}
	        	}
	        	$('#${routeDetailId}-${resType}').html(content);
			}else{
				$.messager.alert('提示',data.msg,'warning');	
			}
	   });
	}
</script>
<div style="margin: 5px;">
	<table id="resGrid" idField="ID"
	        rownumbers="true" pagination="true" pageList="[5,10,15,20]" pageSize="5" 
	        singleSelect="${singleSelect}" striped="true" style="height: 180px;" onLoadSuccess="onSuccess">  
	    <thead>  
	        <tr> 
	        	<th field="ck" checkbox="true"></th>
	        <c:forEach items="${ths}" var="th" varStatus="s"> 
	            <th field="${th}" width="80" <c:if test="${s.index==0}">hidden="true"</c:if>>${th}</th>
	        </c:forEach>
	        	<th field="查看详情" width="70">查看详情</th>  
	        </tr>  
	    </thead>  
	</table>
</div>  
<div style="width: 100%;text-align: center;margin-top: 10px;">
	<a style="text-decoration: none;" id="btn-close" onclick="$('#settingWin').window('close');" class="easyui-linkbutton" iconCls="icon-close">关闭</a>
	<a style="text-decoration: none;" id="btn-add" onclick="addOrUpdate();" class="easyui-linkbutton" iconCls="icon-ok">确定</a>
</div>